import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../../shared/blade/blade-container-base";
import { FormBuilder } from "@angular/forms";
import { ApplicationService } from "../../../../../service/application.service";
import { Observable } from "rxjs/Observable";

import { Permission } from "../../../../../entity/permission.model";
// import { Menu } from "../../../../../entity/menu.model";
// import { MenuService } from "../../../../../service/menu.service";
import { RolePermissionService } from "../../../../../service/role-permission.service";
import { RolePermission } from "../../../../../entity/role-permission.model";
import { PermissionService } from "../../../../../service/permission.service";


@Component({
  selector: 'app-role-permission-edit',
  templateUrl: './role-permission-edit.component.html',
  styleUrls: ['./role-permission-edit.component.css']
})
export class RolePermissionEditComponent extends BladeContainerBase<RolePermission>{


  /**
   * 所有操作权限集合
   * 
   * @type {Permission[]}
   * @memberof RolePermissionEditComponent
   */
  permissions: Permission[] = [];

  /**
   * 角色操作权限集合
   * 
   * @type {RolePermission[]}
   * @memberof RolePermissionEditComponent
   */
  rolePermissions: RolePermission[] = [];

  currentPermission: RolePermission;
  constructor(
    private application: ApplicationService,
    private fb: FormBuilder,
    private rolePermissionService: RolePermissionService,
    private permissionService: PermissionService
  ) {
    super(fb);
  }

  ngOnInit() {
    this.getData();
  }

  getData(): any | Observable<any> | Promise<any> {
    let getPermissions = this.permissionService.getPermissions()
    let getPermissionsByRoleId = this.rolePermissionService.getPermissionsByroleID(this.parentEntity.roleId);
    Promise.all([getPermissions, getPermissionsByRoleId])
      .then(res => {
        if (res[0]) {
          this.permissions = [...res[0]];
        };
        if (res[1]) {
          this.rolePermissions = [...res[1]];
        };
        this.permissions.forEach(item => {
          item.unPermission = false;
        })
        //设置用户权限
        this.rolePermissions.forEach(r => {
          this.permissions.forEach(p => {
            if (r.permissionId == p.permissionId) {
              p.unPermission = true;
            }
          })
        })
      })
      .catch(err => {
        console.log("app-role-Permission-edit" + err)
      })

  }

  //保存
  // onSave(selectedRolePermissions: RolePermission){
  //   console.log(selectedRolePermissions,this.parentEntity)
  // }
  onCreate(selectedPermissions: Permission) {
    this.bladeData.isLoading = true;
    let currentRolePermission = {
      roleId: this.parentEntity.roleId,
      permissionId: selectedPermissions.permissionId,
      unPermission: !selectedPermissions.unPermission
    }
    this.rolePermissionService.create(currentRolePermission)
      .then(res => {
        if (res == 200) {
          let parentContainerInstance = this.parentBlade.bladeContainerInstance;
          parentContainerInstance.refresh(res);
          // this.bladeService.closeBlade(this.currentBlade);
          // this.currentBlade.bladeContainerInstance.refresh(res)
        }
      })
      .catch(error => this.bladeData.error = error)
      .then(res => this.bladeData.isLoading = false);

  }


  onSeletedChange(selectedRolePermissions: RolePermission) {
    this.currentPermission = selectedRolePermissions;
  }
}



// WEBPACK FOOTER //
// ./src/app/module/admin/role/role-permission/role-permission-edit/role-permission-edit.component.ts